﻿using Autofac;
using MintCyclingService.BicycleHandle;
using MintCyclingService.Cycling;
using MintCyclingService.Electronicfence;
using MintCyclingService.JPush;
using MintCyclingService.LogServer;
using MintCyclingService.Remote;
using MintCyclingService.UserAccount;
using MintCyclingService.Utils;
using MintCyclingWebApi.Filter;
using System;
using System.Collections.Generic;
using System.Web.Http;
using static MintCyclingService.Cycling.AddBicycleHardWare_PM;

namespace MintCyclingWebApi.AppControllers
{
    /// <summary>
    /// 车辆模块控制器
    /// </summary>
    public class bikeController : ApiController
    {
        private ICyclingService _cyclingService;
        private IUserAccountService _accountService;
        private IElectronicfenceService _electronicService;
        private IJPushService _JPushService;
        private ILogService _LogService;
        private IRemoteService _RemoteService;

        /// <summary>
        /// 初始化车辆模块控制器
        /// </summary>
        public bikeController()
        {
            _cyclingService = AutoFacConfig.Container.Resolve<ICyclingService>();

            _accountService = AutoFacConfig.Container.Resolve<IUserAccountService>();

            _electronicService = AutoFacConfig.Container.Resolve<IElectronicfenceService>();

            _JPushService = AutoFacConfig.Container.Resolve<IJPushService>();
            _LogService = AutoFacConfig.Container.Resolve<ILogService>();
            _RemoteService = AutoFacConfig.Container.Resolve<IRemoteService>();

        }

        /// <summary>
        /// App首页地图根据输入的地址搜索半径两公里范围内的车辆、电子围栏
        /// 搜索电子围栏内的车辆数和电子围栏外的车信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel searchbyradius([FromBody]NearBicycleEnclosure_PM model)
        {
            return _cyclingService.GetNearBicycleEnclosure(model);
        }


        /// <summary>
        /// 查询是否停放在电子围栏区域
        /// </summary>
        /// <returns></returns>
        [RequestCheck]
        [HttpPost]
        public ResultModel isinfence([FromBody]BicycleIsinRange_PM model)
        {
            return _cyclingService.QueryBicycleIsinRange(model);
        }


        ///// <summary>
        ///// 查询指定地点的车辆信息[暂时废弃]
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //public ResultModel searchbyaddress([FromBody]CoordinateBicycleList_PM model)
        //{
        //    return _cyclingService.GetBicycleListByCoordinate(model);
        //}


        /// <summary>
        /// 服务器查询开锁状态
        /// </summary>
        /// <returns></returns>
        [RequestCheck]
        [HttpGet]
        public ResultModel qunlockstatus([FromUri]string deviceNo)
        {
            return _cyclingService.QueryUnLockBicycleStatus(deviceNo);
        }

        #region 测试接口
        /// <summary>
        /// 测试极光推送-自定义消息(Message) complete Tom
        /// DATE:2017-03-15
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel Test_JPushMessage(string reParams, List<string> registrationList)
        {
            ResultModel result = new ResultModel();
            string Json = _JPushService.SendPostRequest(reParams, registrationList);
            result.ResObject = Json;
            return result;
        }

        /// <summary>
        /// 测试用户-用极光推送通知   complete TOM
        /// DATE:2017-03-14
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel HardwareOpenOrClosekBicycleLock_Test([FromBody]Test_OpenOrCloseLockBicycle_PM model)
        {
            return _cyclingService.HardwareOpenOrClosekLock_Test(model);
        }

        #endregion

        #region  硬件数据上传和开关锁接口

        #region 维护App开锁异常处理

        /// <summary>
        /// 维护App开锁异常处理 complete Tom
        /// DATE:2017-07-27
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel WxCloseLock([FromBody]CloseLock_PM model)
        {
            return _cyclingService.WxCloseLockByLockNumber(model);
        }



        #endregion



        /// <summary>
        /// 远程开锁 complete Tom
        /// DATE:2017-06-22
        /// </summary>
        /// <returns></returns>
        //[RequestCheck]
        [HttpPost]
        public ResultModel SendRemoteOPenLock([FromBody]UnLockBicycle_PM model)
        {
            return _cyclingService.UnLockBicycle(model);
        }

        /// <summary>
        /// 出厂时硬件上传锁信息 complete TOM
        /// DATE:2017-04-27
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel HardWareAddBicycleInfo([FromBody]AddBicycleHardWare_PM model)
        {
            //Utility.Common.FileLog.Log("硬件上传锁信息：时间："+DateTime.Now+",实体信息：" + model, "AddBicycleLog");
            return _cyclingService.AddBicycleLockInfo(model);
        }


        /// <summary>
        /// 12小时锁自动上传锁信息  complete TOM
        /// DATE:2017-05-16
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel HardWareAutoUpadate([FromBody]HardWareAuto_PM model)
        {
            //Utility.Common.FileLog.Log("自动上传锁信息：时间：" + DateTime.Now + ",设备编号：" + model.DeviceNo, "自动上传日志");
           
            return _RemoteService.HardWareAutoUpadate(model);
        }


        /// <summary>
        /// 控制器和电池组硬件锁上传信息[暂时废弃]  complete TOM
        /// DATE:2017-05-19
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel HardWareHelpUpadate([FromBody]ComponentsBinding model)
        {
            //Utility.Common.FileLog.Log("控制器和电池组硬件锁上传信息：时间：" + DateTime.Now + ",锁编号：" + model.LockNumber, "控制器和电池组硬件锁上传");
            return _RemoteService.HardWareHelpUpadate(model);
        }

        /// <summary>
        /// 控制器上报信息  complete TOM
        /// DATE:2017-06-07
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel HardWareControllerReport([FromBody]ControllerReport_PM model)
        {
            //Utility.Common.FileLog.Log("控制器上报信息：时间：" + model.Date + ",锁编号：" + model.LockNumber+",Type:"+model.Type, "ControllerReportLog");
            return _RemoteService.HardWareControllerReport(model);
        }


        /// <summary>
        /// BMS(电池保护板)上报信息  complete TOM
        /// DATE:2017-06-07
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel HardWareBMSReport([FromBody]BMSReport_PM model)
        {
            //Utility.Common.FileLog.Log("BMS上报信息：时间：" + model.Date + ",锁编号：" + model.LockNumber + ",Type:" + model.Type, "BMSReportLog");
            return _RemoteService.HardWareBMSReport(model);
        }

        /// <summary>
        /// 服务器发送给锁远程控制某一个锁骑行模式  complete TOM
        /// DATE:2017-06-09
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel RemoteSendCyclingMode([FromBody]HandleCyclingMode_PM model)
        {
            //Utility.Common.FileLog.Log("服务器发送骑行模式控制指令：时间：" + DateTime.Now + ",锁编号：" + model.LockNumber + ",骑行模式：" + model.RunningMode, "SendCyclingModeLog");
            return _RemoteService.SendHandleCyclingModeByLockNumber(model);
        }

        /// <summary>
        /// 服务器接受锁远程控制模式 complete TOM
        /// DATE:2017-06-09
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel RemoteReceiveCyclingMode([FromBody]HandleCyclingMode_PM model)
        {
            //Utility.Common.FileLog.Log("服务器接受锁远程控制模式：时间：" + DateTime.Now + ",锁编号：" + model.LockNumber + ",骑行模式：" + model.RunningMode, "ReceiveCyclingModeLog");
            return _RemoteService.ReceiveUpdateCylingModeByLockNumber(model);
        }

        /// <summary>
        /// 发送查询控制器与BMS序列号  complete TOM
        /// DATE:2017-06-12
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel SearchControllerBMS([FromBody]SearchControllerBMS_PM model)
        {
            //Utility.Common.FileLog.Log("发送查询控制器与BMS序列号：时间：" + DateTime.Now + ",锁编号：" + model.LockNumber + "", "SendSearchControllerBMSLog");
            return _RemoteService.SendSearchControllerBMS(model);
        }

        /// <summary>
        /// 服务器接受锁远程查询控制器与BMS序列号 complete TOM
        /// DATE:2017-06-23
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel ReceiveSearchControllerBMS([FromBody]ReceiveSearchControllerBMS_PM model)
        {
            //Utility.Common.FileLog.Log("服务器接受锁远程控制模式：时间：" + DateTime.Now + ",锁编号：" + model.LockNumber + ",骑行模式：" + model.RunningMode, "ReceiveCyclingModeLog");
            return _RemoteService.ReceiveSearchControllerBMS(model);
        }


        /// <summary>
        /// 发送远程升级锁程序包  complete TOM
        /// DATE:2017-07-07
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel SendUpgradeBicLockProgram([FromBody]RemoteUpgradeBicLockProgram_PM model)
        {
            return _RemoteService.SendUpgradeBicLockProgram(model);
        }


        /// <summary>
        /// 接受远程升级锁程序包完成后锁的应答  complete TOM
        /// DATE:2017-07-18
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel ReceiveUpgradeFinish([FromBody]ReceiveUpgradeFinish_PM model)
        {
            return _RemoteService.ReceiveUpgradeFinish(model);
        }



        /// <summary>
        /// 上传开关锁交易记录-手机蓝牙或者硬件锁上传 complete TOM
        /// DATE:2017-03-03
        /// </summary>
        /// <returns></returns>
        //[RequestCheck]
        [HttpPost]
        public ResultModel OpenOrClosekBicycleLock([FromBody]OpenOrCloseLockBicycle_PM model)
        {
            return _cyclingService.OpenOrCloseBicycleLockData(model);
        }


        /// <summary>
        /// 输入车辆编号或者扫码开锁获取该车锁的状态 complete Tom
        /// DATE:2017-04-13
        /// </summary>
        /// <returns></returns>
        [RequestCheck]
        [HttpPost]
        public ResultModel CheckInputDeviceNoOrScanCode([FromBody]InputOrScan_PM model)
        {
            return _cyclingService.CheckInputOrScan(model);
        }



        /// <summary>
        /// 获取锁的秘钥串加密信息   complete TOM
        /// 更新时间DATE:2017-05-29
        /// </summary>
        /// <param name="version"></param>
        /// <param name="keySerial"></param>
        /// <param name="deviceNo"></param>
        /// <param name="UserInfoGuid"></param>
        /// <returns></returns>
        [RequestCheck]
        [HttpGet]
        public ResultModel GetEncryptionKey(int version, string keySerial, string deviceNo, Guid UserInfoGuid)
        {
            ResultModel model = new ResultModel();
            GetEncyptionKey_PM para = new GetEncyptionKey_PM
            {
                Version = version,
                KeySerial = keySerial,
                DeviceNo = deviceNo,
                UserInfoGuid = UserInfoGuid
            };
            return _cyclingService.GetEncryptionKey(para);
        }


        #endregion

        #region 预约用车接口
        /// <summary>
        /// 根据车辆编号查询车辆类型  complete TOM
        /// DATE:2017-05-25
        /// </summary>
        /// <param name="BicycleNumber"></param>
        /// <returns></returns>
        [RequestCheck]
        [HttpGet]
        public ResultModel GetBicycleTypeNameByBicycleNumber(string BicycleNumber)
        {
            return _cyclingService.GetBicycleTypeNameByBicycleNumber(BicycleNumber);
        }



        /// <summary>
        /// 添加用户预约用车  complete TOM
        /// DATE:2017-03-21
        /// </summary>
        /// <returns></returns>
        [RequestCheck]
        [HttpPost]
        public ResultModel AddReservationBicycle(AddRegionInfo_PM model)
        {
            ResultModel result = new ResultModel();
            string errorMsg = string.Empty;

            #region 逻辑处理


            //检查预约用户是否已经有其他预约
            if (_cyclingService.CheckUserReservationExist(model.UserGuid))
            {
                return new ResultModel { IsSuccess = false, MsgCode = "0", Message = "您已经预约了其它车辆" };
            }

            //检查用户的余额
            if (!_accountService.CheckUserAvailableBalance(model.UserGuid, out errorMsg))
            {
                result.IsSuccess = false;
                result.Message = errorMsg;
                return result;
            }

            #endregion

            //添加预约用车信息
            return _cyclingService.AddReservation(model);
        }

        /// <summary>
        /// 获取用户的预约倒计时信息  complete TOM
        /// DATE:2017-04-17
        /// </summary>
        /// <returns></returns>
        [RequestCheck]
        [HttpGet]
        public ResultModel GetCountDownTimeByUserGuid([FromUri]Guid? userInfoGuid)
        {
            return _cyclingService.GetCountDownTimeByUserGuid(userInfoGuid);
        }

        /// <summary>
        /// 是否骑行中  complete TOM
        /// DATE:2017-04-21
        /// </summary>
        /// <returns></returns>
        [RequestCheck]
        [HttpGet]
        public ResultModel GetIsRidingByUserGuid([FromUri]Guid? userInfoGuid)
        {
            return _cyclingService.GetIsRidingByUserGuid(userInfoGuid);
        }



        /// <summary>
        /// 取消预约用车  complete TOM
        /// DATE:2017-03-21
        /// </summary>
        /// <returns></returns>
        [RequestCheck]
        [HttpPost]
        public ResultModel CancelReservationBicycle(UpdateReservation_PM model)
        {
            return _cyclingService.CancelReservation(model);
        }

        /// <summary>
        /// 倒计时超时默认预约已结束 complete TOM
        /// DATE:2017-03-22
        /// </summary>
        /// <returns></returns>
        [RequestCheck]
        [HttpPost]
        public ResultModel ReservationOvertimeUpdateStatus(UpdateOvertime_PM model)
        {
            //倒计时超时修改预约已结束
            return _cyclingService.ReservationOvertimeUpdateStatus(model);
        }


        /// <summary>
        /// 服务器定时处理预约超时数据   complete TOM
        /// DATE:2017-04-17
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultModel UpdateReservationStatusByTime()
        {
            return _cyclingService.ReservationTimedTask();
        }


        #endregion

 


    }
}